﻿using Domain;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Utility;
using System.Utility.DBMS;
using System.Utility.Messaging;
using System.Utility.UI;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UI
{
    [System.Runtime.InteropServices.GuidAttribute("D004EA96-8939-4F0E-B7F8-FE82F936E529")]
    public partial class AddOrder : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PopulateStates();
                PopulateCities();
                PopulateTypes();
                PopulateOrderStatus();
            }
        }

        #region Private Members
        private void PopulateStates()
        {
            ddState.DataSource      = DBHelper.GetDataset("GetStates");
            ddState.DataTextField   = "State";
            ddState.DataValueField = "Id";
            
            ddState.DataBind();
        }

        private void PopulateCities()
        {
            ddCity.DataSource = DBHelper.GetDataset("GetCities", new object[] {ddState.Items[ddState.SelectedIndex].Value });
            ddCity.DataTextField = "CityName";
            ddCity.DataValueField = "CityName";

            ddCity.DataBind();
        }
        private void PopulateTypes()
        {
            ddType.DataSource       = DBHelper.GetDataset("GetTypes");
            ddType.DataTextField    = "Type";
            ddType.DataValueField = "Type";

            ddType.DataBind();
        }

        private void PopulateOrderStatus()
        {
            //ddOrderStatus.DataSource = DBHelper.GetDataset("GetOrderStatus");
            //ddOrderStatus.DataTextField = "StatusType";
            //ddOrderStatus.DataValueField = "StatusType";

            //ddOrderStatus.DataBind();
        }
        #endregion

        #region EventHandlers
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                DBHelper.ExcuteProc("AddOrder", new object[] { txtFirstName.Value, txtLastName.Value, txtAddress.Value, ddState.Items[ddState.SelectedIndex].Text,ddCity.Items[ddCity.SelectedIndex].Value, txtZip.Value, 
                    //ddOrderStatus.Items[ddOrderStatus.SelectedIndex].Value,
                    //Convert.ToDecimal(txtOrderSubTotal.Value),Convert.ToDecimal(txtOrderTax.Value),
                    //Convert.ToDecimal(txtOrderShipping.Value),
                    txtNotesToOffice.Value, ddType.Items[ddType.SelectedIndex].Value,
                     StateManager<AppUser>.GetSessionState(SessionKey.UserInfo).Name});
            }
            catch (Exception ex)
            {
                MessageUtility.LogAndGetMessageCode(ex);
                PageUtility.RegisterClientScriptBlockByMessageId(this, "GE0001");
            }
            PageUtility.RefreshByType(new Control[] { txtFirstName, txtLastName, txtAddress, ddState,ddCity,
                txtZip,
                //ddOrderStatus,txtOrderSubTotal,txtOrderTax,txtOrderShipping,
                txtNotesToOffice, ddType });
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            PageUtility.RefreshByType(new Control[] { txtFirstName, txtLastName, txtAddress, ddState,ddCity,
                txtZip,
                //ddOrderStatus,txtOrderSubTotal,txtOrderTax,txtOrderShipping,
                txtNotesToOffice, ddType });
        }
        #endregion
    }
}